﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using NPlot;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort data = new SerialPort("COM3", 600, Parity.None, 7,StopBits.Two);
        //StringBuilder buffer = new StringBuilder();
        List<char> storage = new List<char>();
        private void button1_Click(object sender, EventArgs e)
        {
            timercount = 0;
            data.DtrEnable = true;
            //data.Handshake = Handshake.RequestToSend;
            //data.RtsEnable = true;
            //data.CDHolding;
            //data.RtsEnable = false;
            //data.Handshake = Handshake.RequestToSend;

            data.DataReceived += new SerialDataReceivedEventHandler(data_DataReceived);
            if(!data.IsOpen)
                data.Open();


            
            data.Write("D\r");
            data.Write("D\r");

            /*
            if (data.CtsHolding == true)
            {
                data.Write("D\r");

            }*/
            //data.Write("D\r");
            timer1.Interval = 1000;
            timer1.Start();
        }

        void data_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string inData = sp.ReadExisting();

            foreach (char item in inData.ToCharArray())
            {
                storage.Add(item);
            }
            //buffer.Append(inData);
        }



        List<float> times = new List<float>();
        List<float> values = new List<float>();
        int timercount;
        
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            StringBuilder buffer = new StringBuilder();
            foreach (char item in storage.ToArray())
            {
                if (item != 0)
                {
                    buffer.Append(item);
                }
            }
            
            
            textBox1.Text += buffer.ToString();
            string pattern = @"[-+]?[0-9]*\.?[0-9]+";
            //string pattern = @"\S+";
            //string pattern = @"";
            Regex match = new Regex(pattern);
            //string[] splitBuffer = Regex.Split(buffer.ToString(), pattern);



            string floatToParse = match.Matches(buffer.ToString())[0].Value;

            
            
            
            float voltage = float.Parse(floatToParse);

            //if(match.Matches(buffer.ToString())[2].Value == "mV"){
            //    voltage /= 1000;
            //}
            





            textBox1.Text += Environment.NewLine;
            //char[] buffer = new char[14];
            storage.Clear();
            data.Write("D\r");
            times.Add(timercount);
            values.Add(voltage);
            plotSurface2D1.Clear();
            LinePlot plot = new LinePlot();
            plot.AbscissaData = times;
            plot.OrdinateData = values;
                
            plotSurface2D1.Add(plot);
            timercount++;
            plotSurface2D1.Refresh();
            //Thread.Sleep(1000);
            //data.Write("D\r");
            //data.ReadTimeout = 1000;
            //int a=0;
            //try
            //{
                
            //    data.Read(buffer, 0, 14);
            //}
            //catch (Exception err)
            //{
            //    textBox1.Text = err.Message;
                
            //}
            //String temp = new string(buffer.ToString);
            //textBox1.Text = new String(buffer);
//            textBox1.Text = a.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }
    }
}
